<template>
    <div id="customer">
        <el-button @click="insertList" class="slot-btn-top" type="primary" size="small">添加</el-button>
        <el-table :data="dataList.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="tableHeight" :header-cell-style="getRowClass">
            <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
            <el-table-column prop="bzry" label="任务发布人" sortable min-width="120"></el-table-column>
            <el-table-column prop="szlx" label="收支类型" sortable min-width="110"></el-table-column>
            <el-table-column prop="iename" label="收支名称" sortable min-width="200"></el-table-column>
            <el-table-column fixed="right" label="操作" sortable align="right" width="100">
                <template slot-scope="scope">
                    <el-button @click="updateList(scope.row)" type="text" size="mini">编辑</el-button>
                    <el-button @click="deleteList(scope.row)" type="text" size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="dataList.length"></el-pagination>

        <!-- 基础信息编辑 -->
        <el-dialog :title="dialogTitle" width="750px" center :visible.sync="isDialog" :close-on-click-modal="false">
            <el-form :inline="true" class="demo-form-inline" v-model="dialogList" label-position="right" label-width="120px">
                <el-form-item label="任务发布人"> 

                    <el-select v-model="dialogList.bzry">
                        <el-option v-for='item in szmc' :key='item.id' :label='item.bzry' :value='item.id' ></el-option>
                    </el-select>
                    
                    
                    </el-form-item>
                
                <el-form-item label="收支类型">
                    <el-select v-model="dialogList.szlx" @change='changeSzlx' >
                        <el-option label="收入" value="1"></el-option>
                        <el-option label="支出" value="2"></el-option>
                    </el-select>
                </el-form-item>
               <el-form-item label="收支名称">
                   <el-select v-model="dialogList.iename" >
                       <el-option v-for="item in bzry" :key="item.code" :label="item.name" :value="item.code" ></el-option>
                   </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center;">
                <el-button @click="submitList" type="primary">确 定</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "customer",
        data() {
            let token = sessionStorage.getItem("tokenId");
            return {
                tokenId: token,
                currentPage: 1,
                pagesize: 10,
                tableHeight: document.documentElement.clientHeight - 170,
                dataList: [],
                szmc:[],
                bzry:[],
                isDialog: false,
                dialogTitle: '',
                dialogList:{
                    id:'',
                    bzry: '',
                    szlx: '',
                    iename: '',                   
                },
            }
        },
        created() {
            this.getData();
            this.queryszmc();
            // this.querybzry();
        },
        methods: {
            getData() {
                let _this = this;
                let loading = this.$loading({
                    target: document.querySelector(".wrapper > .content-box"),
                    lock: true,
                    text: '正在努力加载中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.8)'
                });
                this.$http({
                    url: '/api/adscm/operation/baseRysztm/queryAll?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    if (res.data.statusCode == 200) {
                        _this.dataList = res.data.content;
                        loading.close();
                    } else {
                        _this.$message.error(res.data.desc);
                        loading.close();
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                    loading.close();
                })
            },

            changeSzlx(){
                console.log( this.dialogList.szlx )

                    this.querybzry(this.dialogList.szlx)
         


            },

            queryszmc(){
                  let _this = this;
                   this.$http({
                    url: '/api/adscm/operation/baseRysztm/queryBzry?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {

                    if (res.data.statusCode == 200) {
                        _this.szmc = res.data.content;
                      
                    } else {
                        _this.$message.error(res.data.desc);
                       
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                   
                })
            },
              querybzry( id ){
                  let _this = this;
                   this.$http({
                    url: '/api/adscm/operation/baseRysztm/querySzmc?tokenId=' + _this.tokenId+'&szlx='+ id,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {

                    console.log(res.data)

                    if (res.data.statusCode == 200) {
                        _this.bzry = res.data.content;
                      
                    } else {
                        _this.$message.error(res.data.desc);
                       
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                   
                })
            },
            insertList() {
                this.isDialog= true;
                this.dialogTitle= '添加信息';
                this.dialogList= {
                     bzry: '',
                    szlx: '',
                    iename: '',  
                    id:''  
                };
            },
            updateList(row) {
                this.isDialog= true;
                this.dialogTitle= '编辑信息';
                this.dialogList= row;
                this.dialogList.bzry = parseInt( this.dialogList.bzry);


            },
            deleteList(row){
                console.log(row.id)
                let _this = this;
                this.$confirm('确认删除吗?', '提示', {
                    confirmButtonClass: 'el-button--warning'
                }).then(() => {
                    _this.$http({
                       
                        url: '/api/adscm/operation/baseRysztm/delete?id='+row.id+'&tokenId=' + _this.tokenId,
                         method: 'post',
                        headers: {'deviceCode': 'A95ZEF1-47B5-AC90BF3'},
                    }).then(function (res) {
                        if (res.data.statusCode == 200) {
                            _this.getData();
                        } else {
                            _this.$message.error(res.data.desc);
                        }
                    }).catch(function (catchres) {_this.$message.error("加载失败！请重新加载...");})
                }).catch(() => {});
            },
            submitList(){
                let _this = this;
                let url;
                if (this.dialogTitle=== '添加信息') {
                    url='/api/adscm/operation/baseRysztm/insert?tokenId=' + _this.tokenId;
                } else if (this.dialogTitle=== '编辑信息') {
                    url='/api/adscm/operation/baseRysztm/update?tokenId=' + _this.tokenId;
                }
                let param = new URLSearchParams();
                param.append('tokenId', _this.tokenId);
                param.append('data', JSON.stringify(_this.dialogList));
                console.log(_this.dialogList)
                this.$http({
                    url: url,
                    method: 'post',
                    headers: {'deviceCode': 'A95ZEF1-47B5-AC90BF3'}, 
                    data:param
                }).then(function (res) {
                    if (res.data.statusCode == 200) {
                        _this.isDialog= false;
                         _this.getData()
                        } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            handleSizeChange: function (size) {
                this.pagesize = size;
            },
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
            },
        }
    }
</script>

<style scoped>

    #customer .el-table--small {
        font-size: 13px;
    }

    #customer .el-table .cell,
    #customer .el-table th div {
        padding-left: 0;
        padding-right: 0;
    }


</style>
